import Vue from "vue";
import store from "./../store";
import common from "../utils/common";
import Router from "vue-router";


Vue.use(Router);
const router = new Router({
  mode: "history",
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return {
        x: 0,
        y: 0
      };
    }
  },
  routes: [
    {
      path: "/",
      component: () => import("../view/newHome.vue"),
      meta: { title: "title.home"}
    },
    {
      path: "/home",
      name: 'home',
      component: () => import("../view/newHome.vue"),
      meta: { title: "title.home"}
    },
    {
      path: "/quotations",
      name: 'quotations',
      component: () => import("../view/quotations.vue"),
      meta: { title: "title.home"}
    },
    {
      path: "/login",
      component: () => import("../view/login/index"),
      name: "login",
      meta: { title: "title.login" }
    },
    {
      path: "/register",
      component: () => import("../view/login/register"),
      name: "register",
      meta: { title: "title.reg",keepAlive:true }
    },
    {
      path: "/rewardpark",
      component: () => import("../view/newbyla/park.vue"),
      name: "rewardpark",
      meta: { title: "new.desc2" }
    },
    {
      path: "/business",
      component: () => import("../view/newbyla/business.vue"),
      name: "business",
      meta: { title: "new.desc" }
    },
    {
      path: "/business/info",
      component: () => import("../view/newbyla/businessinfo.vue"),
      name: "business",
      meta: { title: "new.desc" }
    },
    {
      path: "/user",
      component: () => import("../view/newbyla/user.vue"),
      name: "user",
      meta: { title: "new.desc1" }
    },
    {
      path: "/user/invite",
      component: () => import("../view/newbyla/invite.vue"),
      name: "invite",
      meta: { title: "new.desc44" }
    },
    {
      path: "/message",
      component: () => import("../view/newbyla/message.vue"),
      name: "message",
      meta: { title: "title.my_news" }
    },
    {
      path: "/messagelist",
      component: () => import("../view/newbyla/messagelist.vue"),
      name: "messagelist",
      meta: { title: "" }
    },
    {
      path: "/messageinfo",
      component: () => import("../view/newbyla/messageinfo.vue"),
      name: "messageinfo",
      meta: { title: "new.desc46" }
    },
    {
      path: "/user/withdrawal",
      component: () => import("../view/newbyla/withdrawal.vue"),
      name: "userwithdrawal",
      meta: { title: "new.desc6" }
    },
    {
      path: "/user/updatepass",
      component: () => import("../view/newbyla/updatepass.vue"),
      name: "updatepass",
      meta: { title: "new.desc8" }
    },
    {
      path: "/user/settrade",
      component: () => import("../view/newbyla/settrade.vue"),
      name: "settrade",
      meta: { title: "new.desc7" }
    },
    {
      path: "/user/wallet",
      component: () => import("../view/newbyla/wallet.vue"),
      name: "userwallet",
      meta: { title: "new.desc36" }
    },
    {
      path: "/businessrecord",
      component: () => import("../view/newbyla/businessrecord.vue"),
      name: "businessrecord",
      meta: { title: "new.desc91" }
    },
    {
      path: "/recordinfo",
      component: () => import("../view/newbyla/recordinfo.vue"),
      name: "recordinfo",
      meta: { title: "new.desc106" }
    },
    {
      path: "/fawry/paystatus",
      component: () => import("../view/newbyla/paystatus.vue"),
      name: "",
      meta: { title: "new.desc106" }
    },
    {
      path: "/user/doc",
      component: () => import("../view/user/doc"),
      meta: { title: "title.user_greement" }
    },
    {
      path: "/user/doc_jp",
      component: () => import("../view/user/doc_jp"),
      meta: { title: "title.user_greement" }
    },
    {
      path: "/member/set_lang",
      component: () => import("../view/member/set_lang"),
      meta: { title: "word.switch_lang" }
    },
    {
      path: "/member/add_bank",
      component: () => import("../view/member/add_bank"),
      meta: { title: "new.doc1" }
    },
    {
      path: "/member/my_balance",
      component: () => import("../view/member/my_balance"),
      meta: { title: "new.doc13" }
    },
    {
      path: "/member/myinfo",
      component: () => import("../view/member/myinfo"),
      meta: { title: "title.myinfo" }
    },
    {
      path: "/member/about",
      component: () => import("../view/member/about"),
      meta: { title: "title.about" }
    },
    {
      path: "/member/help",

      component: () => import("../view/member/help"),
      meta: { title: "title.help" }
    },
    {
      path: "/member/help_view",
      component: () => import("../view/member/help_view"),
      meta: { title: "title.help" }
    },
    {
      path: "/kefu",
      component: () => import("../view/kefu"),
      meta: { title: "title.kefu" }
    },
    {
      path: "/member/my_news_view",
      component: () => import("../view/member/my_news_view"),
      meta: {
        title: "title.my_news_view"
      }
    },
    {
      path: "/member/paylist",
      component: () => import("../view/member/paylist"),
      meta: { title: "充币通道" }
    },
    {
      path: "/member/my_grade",
      component: () => import("../view/member/my_grade"),
      meta: { title: "my_grade.title" }
    },
    {
      path: "/member/my_news",
      component: () => import("../view/member/my_news"),
      meta: { title: "title.my_news" }
    },
    {
      path: "/member/feedback",
      component: () => import("../view/member/feedback"),
      meta: { title: "title.feedback" }
    },
    {
      path: "/income/index",
      component: () => import("../view/income/index"),
      meta: { title: "title.income" }
    },
    {
      path: "/income/list",
      component: () => import("../view/income/list"),
      meta: { title: "title.income" }
    },
    {
      path: "/income/team",
      component: () => import("../view/income/team"),
      meta: { title: "title.team" }
    },
    {
      path: "/agent/index",
      component: () => import("../view/agent/index"),
      meta: { title: "title.agent" }
    },
    {
      path: "/agent/area",
      component: () => import("../view/agent/area"),
      meta: { title: "title.agent2" }
    },
    {
      path: "/doc/index",
      component: () => import("../view/doc/index"),
      meta: { title: "" }
    },
    {
      path: "/doc/downApp",
      component: () => import("../view/doc/downApp"),
      meta: { title: "title.downApp" }
    },

    {
      path: "/doc/kj",
      component: () => import("../view/doc/kj"),
      meta: { title: "title.kj" }
    },
    {
      path: "/doc/lhxy",
      component: () => import("../view/doc/lhxy"),
      meta: { title: "title.lhxy" }
    },
    {
      path: "/bargainrecord",
      component: () => import("../view/doc/bargainrecord"),
      meta: { title: "title.bargainrecord" }
    },
    {
      path: "/inviterecord",
      component: () => import("../view/doc/inviterecord"),
      meta: { title: "title.inviterecord" }
    },
    {
      path: "/inviteinfo",
      component: () => import("../view/doc/inviteinfo"),
      meta: { title: "title.inviteinfo" }
    },
    {
      path: "/bargainrules",
      component: () => import("../view/doc/bargainrules"),
      meta: { title: "title.bargainrules" }
    },
    {
      path: "/profit",
      component: () => import("../view/calculation/profit"),
      meta: { title: "title.profit" }
    },
    {
      path: "/minerprofit",
      component: () => import("../view/calculation/minerprofit"),
      meta: { title: "title.minerprofit" }
    },
    {
      path: "/staff/index",
      component: () => import("../view/staff/index"),
      meta: { title: "staff.doc1" }
    },
    {
      path: "/staff/so",
      component: () => import("../view/staff/so"),
      meta: { title: "staff.doc2" }
    },
    {
      path: "/staff/team",
      component: () => import("../view/staff/team"),
      meta: { title: "staff.doc3" }
    },
    {
      path: "/staff/txmx",
      component: () => import("../view/staff/txmx"),
      meta: { title: "staff.doc4" }
    },
    {
      path: "/staff/czmx",
      component: () => import("../view/staff/czmx"),
      meta: { title: "staff.doc5" }
    },
    {
      path: "/profit/rank",
      component: () => import("../view/profit/rank"),
      meta: { title: "staff.doc5" }
    },
    {
      //路由重定向
      path: "*",
      redirect: "/home"
    }
  ]
});

router.beforeEach((to, from, next) => {
  if (to.query.isApp == 1 || to.query.isApp == 2) {
    if (
      null == to.query.token ||
      undefined == to.query.token ||
      to.query.token.toString().length < 1
    ) {
      store.commit("SET_ISAPPINFO", "");
      store.commit("SET_LOGIN", false);
    } else {
      store.commit("SET_ISAPPINFO", to.query.token);
      store.commit("SET_LOGIN", true);
      //保存用户信息
      store.dispatch("Get_userData");
    }

    if ("ja_JP" == to.query.lang) {
      localStorage.setItem("language", "jp");
    } else if ("en_US" == to.query.lang) {
      //英语
      localStorage.setItem("language", "en");
    } else if ("vi_VN" == to.query.lang) {
      //越南文
      localStorage.setItem("language", "vi_VN");
    } else if ("in_ID" == to.query.lang) {
      //印尼
      localStorage.setItem("language", "in_ID");
    } else if ("ko_KR" == to.query.lang) {
      //韩语
      localStorage.setItem("language", "ko_KR");
    } else if ("th_TH" == to.query.lang) {
      //韩语
      localStorage.setItem("language", "th_TH");
    } else {
      //默认中文
      localStorage.setItem("language", "zh");
    }

  }
  let isLogin = store.state.isLogin;

  if (to.meta.auth) {
    if (!isLogin) {
      if (store.isApp) {
        this.$toast("没有获取到登录状态,请重试！");
        return;
      }

      next("/login");
      return;
    }
  }
  if (to.name === "login" || to.name === "reg" || to.name === "login1") {
    if (to.query.status == undefined) {
      if (isLogin && !store.state.isApp) {
        next("/doc/downApp");
        return;
      }
    }

  }

  //如果是详细页过来的，表明当前页面不需要刷新获取新数据，直接用之前缓存的数据即可
  if (from.name == "shop_detail" || from.name == "shop_home" || from.name == "mearch_alliance_view") {
    to.meta.isBack = true;
  }
  // 跳转前修改福利中心title
  if (to.name == "welfareDetail") {
    to.meta.title = `title.${to.query.title}`
  }

  next();
});
export default router;
